﻿using Microsoft.Extensions.Options;
using Quartz;
using Seer.SeerSocket;


/// <summary>
/// A base class for cron job options. 
/// </summary>
public class GenericCronJobOptions
{
    public string? Schedule { get; set; }
}



[DisallowConcurrentExecution]
// ReSharper disable once ClassNeverInstantiated.Global
public class DetectAgvJob : IJob
{
    private readonly ILogger<DetectAgvJob> logger;
    private readonly SqliteDbContext ctx;
    private readonly IServiceScope serviceScope;
    private readonly SeerSocket seer;
    private readonly IDistributedCacheHelper helper;
    // private readonly GenericCronJobOptions _helloWorldJobOptions;
    public DetectAgvJob(ILogger<DetectAgvJob> logger, IServiceScopeFactory scopeFactory)
    {
        this.serviceScope = scopeFactory.CreateScope();
        var sp = serviceScope.ServiceProvider;
        this.ctx = sp.GetRequiredService<SqliteDbContext>();
        this.logger = sp.GetRequiredService<ILogger<DetectAgvJob>>();
        this.seer = sp.GetRequiredService<SeerSocket>();
        this.helper = sp.GetRequiredService<IDistributedCacheHelper>();
        this.logger = logger;
    }

    public async Task Execute(IJobExecutionContext context)
    {
        await this.DetectBatteryJob();
    }
    private async Task DetectBatteryJob()
    {
        try
        {
            Console.WriteLine($"{DateTime.Now} : detecting");
            var msg = seer.Send(0x044c);
            await helper.UpdateAsync<string>("all1", async e => msg);
        }
        catch (Exception exception)
        {
            Console.WriteLine($"{DateTime.Now} : {exception}");
        }
    }
}



